﻿@model studentPortal.Models.post
@{
    ViewBag.Title = "post";
}
<div class="container">

    <div class="row">
        <div class="col-md-8">
            
            <h1 id="t" style="text-decoration-line:underline">

                <!--get title from DB-->
                @Model.title.ToString()
            </h1>
            <p class="lead">
                by <a href="index.php" id="u">@Model.owner.ToString()</a>
            </p>
            <hr />
            <p>
                <div id="dat">
                    <span class="glyphicon glyphicon-time"></span> Posted on @Model.time.ToString()
                </div>
            </p>

            <hr>
            <div id="d">
                @Model.text.ToString()
            </div>
            <br />
            
            <button class="btn-link" id="like" name="like">Like  </button><span id="likes">@Model.like</span>
            <button class="btn-link" id="dislike" name="dislike">Unlike  </button><span id="dislikes">@Model.dislike</span>

            <hr>
        </div>
    </div>
</div>
<script>
    $('#like').click(function () {
        
        var a = "@Model.postId";
        var c = "@Model.like";
        if ($('#dislike').is(":disabled"))
        {
            $.getJSON("../User/likes?pid=" + a+"&op=ne", function (data) {
                
                if (data == -5) {
                    window.location = "../Account/signin";
                }
                else if (data != -1) {
                    $('#like').css("text-decoration", "none");
                    $('#dislike').attr("disabled", false);
                    $('#likes').text(data);

                }
                else
                {

                }
            });
        }
        else
        {
            $.getJSON("../User/likes?pid=" + a + "&op=pe", function (data) {
                if (data == -5) {
                    
                    window.location = "../Account/signin";
                }
                else if (data != -1) {
                    $('#likes').text(data);
                    $('#dislike').attr("disabled", true);
                    $('#like').css("text-decoration", "line-through");
                }
                else
                {

                }
            });
        }

    });

    $('#dislike').click(function () {
        var a = "@Model.postId";
        var c = "@Model.dislike";
        if ($('#like').is(":disabled")) {
            $.getJSON("../User/unlikes?pid=" + a + "&op=ne", function (data) {
                if (data == -5) {
                    window.location = "../Account/signin";
                }
                else if (data != -1) {
                    $('#dislike').css("text-decoration", "none");
                    $('#like').attr("disabled", false);
                    $('#dislikes').text(data);

                }
                else { }
            });
        }
        else {
            $.getJSON("../User/unlikes?pid=" + a + "&op=pe", function (data) {
                if (data == -5) {
                    window.location = "../Account/signin";
                }
                else if (data != -1) {
                    $('#dislikes').text(data);
                    $('#like').attr("disabled", true);
                    $('#dislike').css("text-decoration", "line-through");

                }
            });
        }

    });


    $(document).ready(function () {

        var a = "@Model.postId";
        $.getJSON("../User/userLike?id=" + a + "&status=post", function (data) {
            
            if (data == 1) {                
                $('#dislike').attr("disabled", true);
                $('#like').css("text-decoration", "line-through");
            }
            else if(data == 0)
            {
                $('#like').attr("disabled", true);
                $('#dislike').css("text-decoration", "line-through");
            }
            else
            {

            }
        });
    });
</script>